/** BCMI_Frame Log Recorder Tool
 *
 *  BCMI_Frame Log Recorder Tool
 *  Define the Log how to record.
 *
 *  @author : huicong
 *  @date : 2008-10-28
 *
 */

#ifndef _LOG_RECORDER_H
#define _LOG_RECORDER_H

#include "BasicHeader.h"
#include "FrameClock.h"

#include <cstdio>
#include <cstdlib>

namespace bcmi_frame
{

    /** log for which method */
    enum LogMethod
    {
        USER, /** log for user function */
        MPI, /** log for mpi function */
        IO, /** log for read or write file */
        MEMORY, /** log for memory use */
        FRAME, /** log for parallel frame */
        JOB_SYSTEM, /** log for job system */
        JOB /** log for job */
        //UNFINISHED
    };

    /**  //UNDO
     *
     *
     */
    class LogRecorder
    {
    public:
        /** Constructor
         *
         * @param [IN] CSTRING _fileName : the name of log file
         * @param [_IN] FrameClock *_clock : system clock
         *
         */
        LogRecorder(IN CSTRING _fileName, FrameClock *_clock);

        ~LogRecorder();

    public:

        /** Main function of log recorder.  
         *
         *  Main function of log recorder. Record the log info.
         *
         *  @param [IN] INT64 _systemTime : system time. default : _logClock
         *  @param [IN] DOUBLE _mpiTime : MPI time. default : _logClock
         *  @param [IN] HRESULT _logStatus : running mode for log. default : H_OK
         *  @param [IN] LogMethod _method : log for which method. default : USER
         *  @param [IN] CSTRING _logWriter : who want to record this log. default : bcmi_frame
         *  @param [IN] VOID* _logInfo : log information buffer to write.
         *  @param [IN] UINT _infoLength : the length of info buffer.
         *  @return HRESULT : log result
         *
         */
        HRESULT log(
            IN INT64 _systemTime,
            IN DOUBLE _mpiTime,
            IN HRESULT _logStatus,
            IN LogMethod _method, 
            IN CSTRING _logWriter,
            IN VOID* _logInfo,
            IN UINT _infoLength);

        /** Default use */
        HRESULT log(IN CSTRING _logInfo);

        /** Default use, may be used by user */
        HRESULT log(IN CSTRING _logWriter, IN CSTRING _logInfo);

        /** Default use, may be used by error or wrong */
        HRESULT log(IN HRESULT _logStatus, IN CSTRING _logWriter, IN CSTRING _logInfo);

        /** Default use, may be used by user on unexpected */
        HRESULT log(
            IN HRESULT _logStatus, 
            IN LogMethod _method, 
            IN CSTRING _logWriter, 
            IN CSTRING _logInfo);

        /** Default use, may be used by system */
        HRESULT log(
            IN HRESULT _logStatus,
            IN LogMethod _method,
            IN CSTRING _logWriter,
            IN VOID* _logInfo,
            IN UINT _infoLength);

        /** Set whether compress save */
        VOID setCompress(IN BOOL _compress);

        /** return whether or not compress save */
        BOOL ifCompressSave();

        VOID logFlush();

    private:
        /** @see Log */
        HRESULT compressLog(
            IN INT64 _systemTime,
            IN DOUBLE _mpiTime,
            IN HRESULT _logStatus,
            IN LogMethod _method, 
            IN CSTRING _logWriter,
            IN VOID* _logInfo,
            IN UINT _infoLength);

        /** @see Log */
        HRESULT uncompressLog(
            IN INT64 _systemTime,
            IN DOUBLE _mpiTime,
            IN HRESULT _logStatus,
            IN LogMethod _method, 
            IN CSTRING _logWriter,
            IN VOID* _logInfo,
            IN UINT _infoLength);


    private:

        FILE* _logFile; // Log file
        FrameClock* _logClock; // Log clock reference to given clock
        BOOL _compressSave; // save in binary for compress

    };

};


#endif //#ifndef _LOG_RECORDER_H